package com.nike.amoeba.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.appcompat.widget.AppCompatTextView;


import com.nike.amoeba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Author nike
 * @Date 2023/6/9 15:03
 * @Description
 */
public class AssetTextAdapter2 extends BaseAdapter {

    private Context act;
    private JSONArray jsonArray;

    public AssetTextAdapter2(Context act, JSONArray jsonArray) {
        this.act = act;
        this.jsonArray = jsonArray;
    }

    @Override
    public int getCount() {
        return jsonArray.length();
    }

    @Override
    public Object getItem(int i) {
        return jsonArray.optJSONObject(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = convertView.inflate(act, R.layout.i_text, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            JSONObject object = jsonArray.getJSONObject(i);

            viewHolder.tv_title.setText(addSpaceBeforeUpperCase(object.optString("title")) + "：");
            viewHolder.tv_content.setText(object.optString("content"));

            if (object.optString("title").equals("InventoryStatus")){
                if (object.optInt("content") == 1){
                    viewHolder.tv_content.setText(act.getText(R.string.found));
                }else {
                    viewHolder.tv_content.setText(act.getText(R.string.missing));
                }
            }
            if (object.optString("title").equals("scanStatus")){
                if (object.optInt("content") == 116){
                    viewHolder.tv_content.setText("RFID");
                }else if (object.optInt("content") == 117){
                    viewHolder.tv_content.setText("QRCode");
                }else if (object.optInt("content") == 118){
                    viewHolder.tv_content.setText("Manually");
                }else {
                    viewHolder.tv_content.setText("");
                }
            }
            if (object.optString("title").equals("FoundStatus")){
                if (object.optInt("content") == 116){
                    viewHolder.tv_content.setText("RFID");
                }else if (object.optInt("content") == 117){
                    viewHolder.tv_content.setText("QRCode");
                }else if (object.optInt("content") == 118){
                    viewHolder.tv_content.setText("Manually");
                }else {
                    viewHolder.tv_content.setText("");
                }
            }

        } catch (JSONException e) {
            throw new RuntimeException("InventoryDesc2Adapter go go go");
        }
        return convertView;
    }

    private String addSpaceBeforeUpperCase(String input) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (i > 0 && Character.isUpperCase(currentChar)) {
                result.append(" ");
            }
            result.append(currentChar);
        }
        return result.toString();
    }

    class ViewHolder{

        AppCompatTextView tv_title;
        AppCompatTextView tv_content;

        public ViewHolder(View convertView) {
            tv_title = convertView.findViewById(R.id.tv_title);
            tv_content = convertView.findViewById(R.id.tv_content);
        }

    }

}
